"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FlowcommentCommentCtrl
 * @description
 * # FlowcommentCommentCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "FlowcommentCommentCtrl",
    function(DataService, openApi, $location, localStorageService, Util, $routeParams) {
      var vm = this;
      var init = function() {
        vm.data = localStorageService.get("flowcomment") || {};
        localStorageService.remove("flowcomment");
        vm.data.followList = localStorageService.get("chooseUsrs");
        localStorageService.remove("chooseUsrs");
        getComment();
      };
      this.delpersons = function(index) {
        vm.data.followList.splice(index, 1);
      };
      var getComment = function() {
        var param = {};
        param.refId = $routeParams.uid;
        var formKey = Util.getformKey();
        param.formKey = formKey;
        DataService.getData(openApi.getComment, param).then(function(data) {
          console.log(data);
          vm.comments = data;
        });
      };
      this.addomment = function() {
        if (!this.data.content) {
          Util.Toast("请输入评论内容");
          return;
        }
        this.data.refId = $routeParams.uid;
        this.data.formKey = Util.getformKey();
        if (this.data.followList) {
          this.data.follows = this.data.followList.map(function(data) {
            return data.userId;
          });
        }

        DataService.getData(openApi.addComment, this.data).then(function(data) {
          if (data) {
            Util.Toast("添加评论成功");
            vm.comments.unshift(data);
            vm.data = {};
          }
        });
      };
      this.choosepeople = function() {
        var userskey = {
          iconCls: "portrait",
          name: "personName",
          id: "userId"
        };
        localStorageService.set("userskey", userskey);
        localStorageService.set("chooseUsrs", vm.data.followList);
        localStorageService.set("flowcomment", vm.data);
        // getFollows是要访问的接口
        $location.path("chooseuser/index/getFollows");
      };

      init();
    }
  );
